package com.jiankang.controller;

import java.io.File;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jiankang.util.Data;
@Controller
@RequestMapping("/PublicController")
public class PublicController {

	//	上传图片
	@RequestMapping(value="/uploadPic")
	@ResponseBody
	public Object updatePic(MultipartFile file) throws Exception {
		JSONObject json=new JSONObject();
		//原始名称
		String originalFilename = file.getOriginalFilename();
		//上传图片
		if(file!=null && originalFilename!=null && originalFilename.length()>0){
			//存储图片的物理路径
			String base = this.getClass().getResource("/").getPath()+"../../upload/cache/";
			//新的图片名称
			String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			//新图片
			File newFile = new File(base+newFileName);
			//将内存中的数据写入磁盘
			file.transferTo(newFile);
			json.put("message",Data.URL.base+"upload/cache/"+newFileName);
			return json;
		}
		else{
			json.put("message","图片上传出现问题");
			return json;
		}
	}

}
